<template>
  <div :class="prefixCls" class="relative h-[100%] lt-sm:px-10px lt-md:px-10px lt-xl:px-10px">
    <div class="relative mx-auto h-full flex">
      <!--
        xs: '480px', sm: '640px', md: '768px', lg: '1024px', xl: '1280px', 2xl: '1536px'
        lt-md: 屏幕宽度小于md时(max-width: 767.9px)，md: 屏幕宽度大于等于md时(min-width: 768px)，at-md: 屏幕宽度大于等于md且时小于lg时(min-width: 768px, max-width: 1023.9px)
      -->
      <div
        :class="`${prefixCls}__left flex-1 bg-gray-500 bg-opacity-20 relative p-30px lt-xl:hidden overflow-x-hidden overflow-y-auto`"
      >
        <!-- 左上角的 logo、系统标题 -->
        <div class="relative flex items-center text-white">
          <img alt="" class="mr-10px h-48px w-48px" src="@/assets/imgs/logo.png" />
          <span class="text-20px font-bold">{{ appStore.getTitle }}</span>
        </div>
        <!-- 左边的背景图、欢迎语 -->
        <div class="h-[calc(100%-60px)] flex items-center justify-center">
          <TransitionGroup
            appear
            enter-active-class="animate__animated animate__bounceInLeft"
            tag="div"
          >
            <img key="1" src="@/assets/svgs/login-box-bg.svg" alt="" />
            <div key="2" class="text-3xl text-white">{{ t('login.welcome') }}</div>
            <div key="3" class="mt-5 text-14px font-normal text-white"
              >{{ t('login.message') }}
            </div>
          </TransitionGroup>
        </div>
      </div>
      <div
        class="relative flex-1 p-30px dark:bg-[var(--login-bg-color)] lt-sm:p-10px overflow-x-hidden overflow-y-auto"
      >
        <!-- 右上角的主题、语言选项 -->
        <div
          class="flex items-center justify-between at-2xl:justify-end at-xl:justify-end color-[var(--el-text-color-primary)]"
        >
          <div class="flex items-center at-2xl:hidden at-xl:hidden">
            <img alt="" class="mr-10px h-48px w-48px" src="@/assets/imgs/logo.png" />
            <span class="text-20px font-bold">{{ appStore.getTitle }}</span>
          </div>
          <div class="flex items-center justify-end space-x-10px h-48px">
            <ThemeSwitch />
            <LocaleDropdown />
          </div>
        </div>
        <!-- 右边的登录界面 -->
        <Transition appear enter-active-class="animate__animated animate__bounceInRight">
          <div
            class="m-auto h-[calc(100%-60px)] w-[100%] flex items-center at-2xl:max-w-500px at-lg:max-w-500px at-md:max-w-500px at-xl:max-w-500px"
          >
            <!-- 账号登录 -->
            <PasswdLoginForm class="m-auto h-auto p-20px lt-xl:(rounded-3xl light:bg-white)" />
            <SmsCodeLoginForm class="m-auto h-auto p-20px lt-xl:(rounded-3xl light:bg-white)" />
            <ResetPasswdForm class="m-auto h-auto p-20px lt-xl:(rounded-3xl light:bg-white)" />
          </div>
        </Transition>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useDesign } from '@/hooks/web/useDesign'
import { useAppStore } from '@/stores/modules/app'
import { ThemeSwitch } from '@/layout/components/ThemeSwitch'
import { LocaleDropdown } from '@/layout/components/LocaleDropdown'
import PasswdLoginForm from './components/PasswdLoginForm.vue'
import SmsCodeLoginForm from './components/SmsCodeLoginForm.vue'
import ResetPasswdForm from './components/ResetPasswdForm.vue'
import * as LoginApi from '@/api/base/login'
import * as authUtil from '@/utils/auth'

defineOptions({ name: 'Login' })

const { t } = useI18n()
const appStore = useAppStore()
const { getPrefixCls } = useDesign()
const prefixCls = getPrefixCls('login')

const tenantEnable = import.meta.env.VITE_APP_TENANT_ENABLE === 'true'

const getTenantByWebsite = async () => {
  // 根据域名获取租户信息
  if (tenantEnable) {
    // 根据域名定位tenantId
    const data = await LoginApi.getTenantByWebsite(location.host)
    if (data) {
      authUtil.setTenantId(data.id)
    }
  }
}

onMounted(() => {
  getTenantByWebsite()
})
</script>

<style scoped lang="scss">
$prefix-cls: #{$namespace}-login;

.#{$prefix-cls} {
  overflow: auto;

  &__left {
    &::before {
      position: absolute;
      top: 0;
      left: 0;
      z-index: -1;
      width: 100%;
      height: 100%;
      background-image: url('@/assets/svgs/login-bg.svg');
      background-position: center;
      background-repeat: no-repeat;
      content: '';
    }
  }
}
</style>

<style lang="scss">
.dark .login-form {
  .el-divider__text {
    background-color: var(--login-bg-color);
  }

  .el-card {
    background-color: var(--login-bg-color);
  }
}
</style>
